package com.ray.shiro;

import java.util.Set;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.rpc.RpcException;
import com.ray.api.service.UcenterApiService;
import com.ray.auth.system.table.entity.User;
import com.xiaoleilu.hutool.util.ObjectUtil;
import com.xiaoleilu.hutool.util.StrUtil;
import com.zjhc.flyhong.common.function.exception.BussinessException;
import com.zjhc.flyhong.common.web.Result;
import com.zjhc.shiro.db.RealmDataBaseRead;
import com.zjhc.shiro.entity.ShiroUser;

/**
 * <h1>自定义读取数据库信息 </h1>
 * @创建人 ray
 * @创建时间 2017-06-12
 * @版本 1.0
 * @重要修改备注 
 * 
 */
public class HongWebRealm implements RealmDataBaseRead{
	private static final Logger LOGGER = Logger.getLogger(HongWebRealm.class);

	@Autowired
    private UcenterApiService ucenterApiService;
	
	
	
	
	public ShiroUser queryUser(String userName) {
		ShiroUser shiroUser = null;
		try {
			if(StrUtil.isNotBlank(userName)){
				//获取用户信息
				Result<User> userResullt = ucenterApiService.queryUser(userName);
				if(userResullt.codeSuccess()){
					User user  = userResullt.getData();
					if(ObjectUtil.isNotNull(user)){
						shiroUser = new ShiroUser();
						shiroUser.setUserName(userName);
						shiroUser.setPassword(user.getPassword());
						shiroUser.setExpendValue(user);
					}
				}else{
					throw new BussinessException(userResullt.getCode(), userResullt.getMsg());
				}
				
			}
		} catch (RpcException e) {//Rpc异常
			LOGGER.error("RPC调用异常");
		}catch (Exception e) {
			LOGGER.error("内部异常");
		}
		return shiroUser;
	}

	
	public Set<String> queryResours(String userName) {
		//放置请求路径 及页面路径
		return null;
	}

}
